package com.dody.agile.payment.schedule;

import java.util.Calendar;
import java.util.Date;

/**
 * @author ShiXin
 * @date 2019/4/15 21:37
 * @description agile
 */
public class MonthlySchedule implements PaymentSchedule {
    @Override
    public boolean isPayDay(Date date) {
        return isLastDayOfMonth(date);
    }

    @Override
    public Date getPayPeriodStartDate(Date payPeriodEndDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(payPeriodEndDate);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        return calendar.getTime();
    }


    private boolean isLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int m1 = calendar.get(Calendar.MONTH);
        calendar.add(Calendar.DAY_OF_MONTH,1);
        int m2 = calendar.get(Calendar.MONTH);
        return m1 != m2;
    }

    public static void main(String[] args) {
        MonthlySchedule monthlySchedule = new MonthlySchedule();
        Calendar calendar = Calendar.getInstance();
        calendar.set(2019,Calendar.APRIL,30);
        System.out.println(monthlySchedule.isLastDayOfMonth(calendar.getTime()));
        System.out.println(monthlySchedule.getPayPeriodStartDate(calendar.getTime()));
    }
}
